# -*- coding: utf-8 -*-
import scrapy
import json
import time
from lianjia.items import Community
from lianjia.geohash import *


class XiaoquSpider(scrapy.Spider):
    name = "xiaoqu"
    allowed_domains = ["lianjia.com", "qq.com"]
    start_urls = (
        'http://bj.lianjia.com/xiaoqu/pg1/',
        # 'http://sh.lianjia.com/xiaoqu/pg1',
        # 'http://gz.lianjia.com/xiaoqu/pg1',
        'http://sz.lianjia.com/xiaoqu/pg1',
        'http://hz.lianjia.com/xiaoqu/pg1',
        'http://cd.lianjia.com/xiaoqu/pg1',
    )
    cities = {'bj.lianjia.com': u'北京', 'sh.lianjia.com': u'上海', 'gz.lianjia.com': u'广州',
              'sz.lianjia.com': u'深圳', 'hz.lianjia.com': u'杭州', 'cd.lianjia.com': u'成都'}

    def parse(self, response):
        city = self.cities[response.url.split("/")[2]]
        for xiaoqu in response.xpath('//div[@class=\'info-panel\']'):
            community = Community()
            community['name'] = xiaoqu.xpath('h2/a/text()').extract()[0]
            url = "http://apis.map.qq.com/ws/place/v1/search?keyword=" + community['name'] \
                  + "&boundary=region(" + city + ",0)&key=RIYBZ-ULTRI-LAYGX-5KH7B-FJVQ2-3JBZ7"
            price = xiaoqu.xpath('.//div[@class=\'price\']/span[@class=\'num\']/text()').extract()
            if len(price) > 0:
                community['price'] = price[0]
                request = scrapy.Request(url, self.parseGeo)
                request.meta['item'] = community
                time.sleep(0.3)
                yield request
        page = json.loads(response.xpath('//div[@class=\'page-box house-lst-page-box\']/@page-data').extract()[0])
        if page['curPage'] < page['totalPage']:
            url = response.url.replace(str(page['curPage']), str(page['curPage'] + 1))
            yield scrapy.Request(url, self.parse)

    def parseGeo(self, response):
        item = response.meta['item']
        geo = json.loads(response.body_as_unicode())
        if len(geo['data']) > 0:
            location = geo['data'][0]['location']
            item['lat'] = location['lat']
            item['lng'] = location['lng']
            item['geohash'] = encode(location['lat'], location['lng'])
        yield item
